﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config;
using Microsoft.Samples.Office.UnifiedCommunications.Gadget.DTO;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public class DesktopContactCollection : ObservableCollection<DesktopContact>
    {
        public DesktopContactCollection()
        {
            DesktopContactElementCollection desktopContacts = GadgetConfigurationManager.GetDesktopContacts();

            int maxContacts = 5; // The gadget is limited to 5 contacts

            //  A call to GetByContactId will return null if no element exists for that order key.
            //  Attempt to get all elements, always gives us a collection of 5 items.
            //  Just re-instantiate this every time we need the latest collection.

            for (int i = 1; i <= maxContacts; i++)
            {
                DesktopContactElement desktopContactElement = desktopContacts.GetByContactId(i);

                DesktopContact desktopContact;

                if (desktopContactElement == null)
                    desktopContact = new DesktopContact()
                    {
                        ContactId = i,
                        SipUri = default(string),
                        DisplayName = default(string),
                        Visible = default(bool)
                    };
                else
                    desktopContact = new DesktopContact()
                    {
                        ContactId = desktopContactElement.ContactId,
                        SipUri = desktopContactElement.SipUri,
                        DisplayName = desktopContactElement.DisplayName,
                        Visible = desktopContactElement.Visible
                    };

                this.Add(desktopContact);
            }
        }
    }
}
